package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.TMemberMapper;
import com.itheima.dao.TOrderMapper;
import com.itheima.dao.TOrdersettingMapper;
import com.itheima.dao.TSetmealMapper;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderService.class)
public class OrderServiceImpl implements OrderService {
    @Autowired
    private TOrderMapper orderMapper;

    @Autowired
    private TOrdersettingMapper ordersettingMapper;

    @Autowired
    private TMemberMapper memberMapper;

    @Override
    public Result order(Map map) throws Exception {
        String orderDate =(String) map.get("orderDate");
        Date parse = new SimpleDateFormat("yyyy-MM-dd").parse(orderDate);

        // 查询是否可以预约
        OrderSetting orderSetting = ordersettingMapper.findByOrderDate(parse);
        if(orderSetting == null ){
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }
        long reservations = ordersettingMapper.findReservationsByOrderDate(parse);
        if (orderSetting.getNumber() <= reservations ){
            return new Result(false, MessageConstant.ORDER_FULL);
        }

        String telephone = (String) map.get("telephone");
        Member member = memberMapper.findByPhone(telephone);
        if (member != null ){
            Order order = new Order(member.getId(), parse, Integer.parseInt(map.get("setmealId").toString()));

            List<Order> list = orderMapper.findOrder(order);
            if (list != null && list.size() > 0){
                return new Result(false, MessageConstant.HAS_ORDERED);
            }
        }else {
            member = new Member();
            member.setName((String) map.get("name"));
            member.setPhoneNumber(telephone);
            member.setIdCard((String) map.get("idCard"));
            member.setSex((String) map.get("sex"));
            member.setRegTime(new Date());
            memberMapper.insert(member);//自动完成会员注
        }

        //5、预约成功，更新当日的已预约人数
        Order order = new Order();
        order.setMemberId(member.getId());//设置会员ID
        order.setOrderDate(DateUtils.parseString2Date(orderDate));//预约日期
        order.setOrderType((String) map.get("orderType"));//预约类型
        order.setOrderStatus(Order.ORDERSTATUS_NO);//到诊状态
        order.setSetmealId(Integer.parseInt((String) map.get("setmealId")));//套餐ID
        orderMapper.add(order);

        orderSetting.setReservations(orderSetting.getReservations() + 1);//设置已预约人数+1
        ordersettingMapper.editReservationsByOrderDate(orderSetting);

        return new Result(true,MessageConstant.ORDER_SUCCESS,order.getId());
    }

    //根据预约ID查询预约相关信息（体检人姓名、预约日期、套餐名称、预约类型）
    public Map findById(Integer id) throws Exception{
        Map map = orderMapper.findById4Detail(id);
        if(map != null){
            //处理日期格式
            Date orderDate = (Date) map.get("orderDate");
            map.put("orderDate",DateUtils.parseDate2String(orderDate));
        }
        return map;
    }
}
